﻿#define CPPHTTPLIB_OPENSSL_SUPPORT
#include <httplib.h>
#include <iostream>
#include <fstream>
#include<queue>
#include<regex>
#include "parse.h"
using namespace std;
void show()
{
    cout << "                            ****************************************" << endl;
    cout << endl;
    cout << "                                        百合会漫画下载器" << endl;
    cout << endl;
    cout << "                            ****************************************" << endl;
}
static void only_char(std::string url)
{
    Parse http(url);
    Html_Parse ps(http.host, http.path, http.cookie, http.refer);
    if (!ps.html_Get())
    {
        cout << "获取网页正文错误" << endl;
    }
    int i = 0;
    while (i<5)
    {
        int i = 1;
        if (!ps.find_file(ps.name_file) || !ps.dir_find())
        {
            cout << "获取文件出错，重试次数"<<i << endl;
            i++;
            Sleep(2000);
        }
        break;
    }
    if (i == 5)
    {
        cout << "错误信息已写入本地文件: error_file.txt" << endl;
        fstream er_fil("error_file.txt", std::ios::in | std::ios::app);
        if (er_fil.is_open() == 0)
        {
            cout << "错误文件创建失败" << endl;
        }
        er_fil.write(url.c_str(), url.size());
    }
    ps.file_download();
}

int  Check_Url(std::string url)
{
    size_t pos = url.find("search");
    if (pos != std::string::npos)
    {
        return 2;
    }
         pos = url.find("yamibo");
        if (pos != std::string::npos)
        {
            return 1;
        }

    
    return 0;
}
std::string show_windows()
{
    //显示窗口
    show();
    string url;
    url.resize(100);
    cin >> url;
    while (true)
    {
        if (Check_Url(url)!=0)
        {
            break;
        }
        url.clear();

        cin.clear();
        cin.sync();//清空缓冲区
        cin >> url;
    }
    return url;
}

void url_List(std::string &url,queue<std::string>& url_list)
{
    Parse http(url);
    Html_Parse ps(http.host, http.path, http.cookie, http.refer);
    if (ps.html_Get() == 0)
    {
        cout << "获取搜索网页出错  退出" << endl;
        exit(0);
   }
    std::string& body = ps.body;
    regex reg("<a href=\".*arget=\"_blank\" >");
    smatch cat;
    auto beg = body.cbegin();
    auto eng = body.cend();
    while (regex_search(beg, eng, cat, reg))
    {
        std::string s = cat.str();
        regex ifront("<a href=\"");
        s = regex_replace(s, ifront, "/");
        regex iafter("\" target=\"_blank\" >");
        s = regex_replace(s, iafter, "");
        url_list.push(s);
        beg = cat.suffix().first;
    }
}
int main(void) {
    
    std::string url = show_windows();
    switch(Check_Url(url))
    {
        case 1:
        {
            cout << "单章下载" << endl;
            only_char(url);
            break;
        }
        case 2:
        {
            cout << "搜索结果下载" << endl;
            queue<std::string> url_list;
            url_List(url, url_list);

            cout << "**********************************************************************************" << endl;
            cout << "任务数量" << url_list.size() << endl;
            int num = url_list.size()+1;
            for (int i = 1; i < num; i++)
            {
                std::string url_path= url_list.front();
                url_list.pop();
                std::string url_one= "https://bbs.yamibo.com";

                url_one += url_path;
                regex gex("amp;");
                url_one=regex_replace(url_one, gex, "");
                only_char(url_one);
                cout << "任务 " << i << "下载完毕" << endl;
                cout << "**********************************************************************************" << endl;
                Sleep(2000);
            }
            break;
        }

    }
    cout << "10秒后关闭" << endl;
    Sleep(10000);
    return 0;
}
